package com.dz.controller.user;

import com.dz.constant.MessageConstant;
import com.dz.constant.StatusConstant;
import com.dz.context.BaseContext;
import com.dz.dto.OrdersSubmitDTO;
import com.dz.dto.SubmitDTO;
import com.dz.entity.Orders;
import com.dz.entity.ShoppingCart;
import com.dz.redis.RedisCache;
import com.dz.result.Result;
import com.dz.service.OrderService;
import com.dz.service.ShopService;
import com.dz.service.ShoppingCartService;
import com.dz.vo.OrderSubmitVO;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: DengHaoQi
 * @date: 2024/4/12 19:04
 * @description: 订单相关接口
 */

@Api(tags = "订单接口")
@RestController
@RequestMapping("/user/order")
@RequiredArgsConstructor
public class OrderController {

    private final ShoppingCartService shoppingCartService;

    private final RedisCache redisCache;

    private final OrderService orderService;

    private final ShopService shopService;

    /*
    * 用户下单
    * */
    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> userSubmit(@RequestBody @Validated OrdersSubmitDTO dto) {
        // 首先判断店铺是否打烊
        Integer status = shopService.getShopStatus().getData();
        if (Objects.equals(status, StatusConstant.DISABLE)) {
            return Result.error(MessageConstant.SHOP_CLOSE);
        }

        // 当前下单的用户id
        Long userID = BaseContext.getCurrentId();
        Assert.notNull(userID, MessageConstant.PARAM_ERROR);

        // 查询购物车的商品信息
        List<ShoppingCart> shoppingCarts = shoppingCartService.getList(userID);
        List<Long> ids = shoppingCarts
                .stream()
                .map(ShoppingCart::getId)
                .toList();
        Assert.notNull(ids,MessageConstant.PARAM_ERROR);
        String idsStr = ids.stream()
                .map(Object::toString)
                .collect(Collectors.joining());

        // 生成订单号,由用户id和商品id组成，保证唯一
        String orderNum = userID + idsStr;

        // 用户id+订单id作为key，查询redis
        Object cacheObject = redisCache.getCacheObject(orderNum);
        if (cacheObject instanceof Orders) {
            // 表示订单重复提交
            return Result.error(MessageConstant.ORDER_IS_EXIST);
        }

        // 没有数据，插入数据库并写入redis，设置5秒的过期时间
        SubmitDTO submitDTO = SubmitDTO.builder()
                .userId(userID)
                .orderNum(orderNum)
                .ordersSubmitDTO(dto)
                .build();
        OrderSubmitVO submitVO = orderService.submit(submitDTO);
        return Objects.nonNull(submitVO) ? Result.success(submitVO) : Result.error(MessageConstant.ORDER_SUBMIT_FAIL);
    }

}
